package com.vertx.user.store;

import com.vertx.core.annotation.data.Address;
import com.vertx.core.annotation.data.Query;
import com.vertx.core.common.ResultFormat;
import com.vertx.core.common.StatusCodeMsg;
import com.vertx.data.entity.SqlPropertyValue;
import com.vertx.data.mysql.AbstractSQL;
import com.vertx.data.utils.DbUtils;
import com.vertx.user.model.User;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;

import java.util.List;

/**
 * 描述:
 * 创建人:hzw
 * 创建时间: 2018/4/26 14:28
 **/
@Query(name = "UserStore")
public class UserStore extends AbstractSQL<User>{

    @Override
    protected String tableName() {
        return "t_user";
    }

    @Override
    protected String primaryId() {
        return "id";
    }

    @Override
    protected String columns() {
        return " id AS id, name AS name,phone as phone";
    }

    @Override
    protected List<SqlPropertyValue<?>> propertyValue(User obj) {
        return null;
    }


    @Address(path = "getUser")
    public void getUser(JsonObject jsonObject, Message<JsonObject> handler) {
        DbUtils.getJdbcClient().getConnection(db->{
            if(db.succeeded()){
                selectAll(db.result(),rsp->{
                    if(!rsp.succeeded()){
                        handler.reply(ResultFormat.format(StatusCodeMsg.C200,rsp.result()));
                    }else{
                        handler.reply(ResultFormat.formatAsNewArray(StatusCodeMsg.C1500));
                    }
                });
            }else{
                handler.fail(500,"fail");
            }
        });
    }
}
